package com.ydkfjs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydkfjs.model.dto.DailyRequest;
import com.ydkfjs.model.po.Daily;
import com.ydkfjs.model.vo.DailyAllVO;
import com.ydkfjs.model.vo.DailyVO;
import com.ydkfjs.service.DailyService;
import com.ydkfjs.mapper.DailyMapper;
import com.ydkfjs.utils.HBAssert;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hb
 * @description 针对表【t_daily】的数据库操作Service实现
 * @createDate 2024-04-02 10:34:56
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily>
        implements DailyService {

    private final DailyMapper dailyMapper;

    public DailyServiceImpl(DailyMapper dailyMapper) {
        this.dailyMapper = dailyMapper;
    }

    @Override
    public void updateById(Long id, DailyRequest dailyRequest) {
        // DB类型转换
        Daily daily = BeanUtil.copyProperties(dailyRequest, Daily.class);

        // 更新数据库记录
        int update = dailyMapper.update(daily, new LambdaUpdateWrapper<Daily>()
                .eq(Daily::getId, id));

        // 判断操作是否正确
        HBAssert.isTrue(update > 0, "更新失败");
    }

    @Override
    public DailyAllVO getByData(LocalDate localDate) {
        // 根据日期查询
        List<Daily> dailies = dailyMapper.selectList(new LambdaQueryWrapper<Daily>()
                .eq(Daily::getDate, localDate));

        // VO类型转换
        List<DailyVO> dailyVOS = BeanUtil.copyToList(dailies, DailyVO.class);

        // 蛋白质摄入量
        int protein = 0;
        // 脂肪摄入量
        int fat = 0;
        // 碳水摄入量
        int cabs = 0;
        // 摄入卡路里(热量)
        int caloriesIn = 0;
        // 消耗卡路里(热量)
        int caloriesOut = 0;

        //遍历所有日常记录,获得各项指标总值i
        for (DailyVO dailyVO : dailyVOS) {
            if (dailyVO.getMeal() != 5) {
                protein += dailyVO.getProtein();
                fat += dailyVO.getFat();
                cabs += dailyVO.getCabs();
                caloriesIn += dailyVO.getCalories();
            } else {
                caloriesOut += dailyVO.getCalories();
            }
        }

        // 创建返回对象
        DailyAllVO dailyAllVO = new DailyAllVO(protein, fat, cabs, caloriesIn, caloriesOut, dailyVOS);

        return dailyAllVO;
    }

    @Override
    public void add(DailyRequest dailyRequest) {
        // DB类型转换
        Daily daily = BeanUtil.copyProperties(dailyRequest, Daily.class);

        // 存入数据库
        int insert = dailyMapper.insert(daily);

        // 判断操作是否正确
        HBAssert.isTrue(insert > 0, "新增失败");
    }

    @Override
    public void deleteById(Long id) {
        // 根据id删除
        int i = dailyMapper.deleteById(id);

        // 判断操作是否正确
        HBAssert.isTrue(i > 0, "删除失败!");
    }
}




